<?php
namespace app\manage\controller;
use app\common\model\User;
use app\common\model\Message;
use app\common\model\China;
use app\common\model\Relation;
class Index extends Common
{
    /**
     * 个人信息
     * @return [type] [description]
     */
    public function index(){
        $uid = session('user.id');
        $place = config('PLACE');

        $userModel = new User();
        $chinaModel = new China();
        $relationModel = new Relation();

        $info = $userModel->getOne('*',['uid'=>$uid]);

        $info['addres'] = '';
        if($info['pid']){
            $pid = $chinaModel->areas($info['pid']);
            $info['addres'] .= $pid['name'] . '/';
            if($info['cid']){
                $cid = $chinaModel->areas($info['cid']);
                $info['addres'] .= $cid['name'] . '/';
                if($info['aid']){
                    $aid = $chinaModel->areas($info['aid']);
                    $info['addres'] .= $aid['name'];
                }
            }
        }
        $info['role'] = $relationModel->identity($uid, 1, 1);
        $info['note'] = json_decode($info['note'],true);
        $info['photo'] = $info['photo'] ? '/uploads/' . $info['photo'] : '';

        if ($info['note']) {
            $img = ['id_just', 'id_back', 'contract_1', 'contract_2', 'contract_3', 'cert_d', 'cert_z', 'cert_l', 'cert_2', 'cert_3', 'cert_4', 'license_1', 'license_2'];

            $keys = array_keys($info['note']);
            foreach ($keys as $key) {
                if (in_array($key, $img)) {
                    $info['note'][$key] = '/uploads/' . $info['note'][$key];
                }
            }
        }

        $mans = $relationModel->get_resource_man($uid); // 资源人
        $res = [
            'place' => $place,
            'data'  => $info,
            'mans'  => $mans,
            'title' =>'个人信息'
        ];

        return view('',$res);
    }

    /**
     * 密码修改
     * @return [type] [description]
     */
    public function upwd(){
        if (request()->isPost()){
            $uid = session('user.id');
            $opwd=input('post.opwd',false);
            $pwd=input('post.pwd',false);
            $rpwd=input('post.rpwd',false);
            if ($pwd!==$rpwd){
                show(false,'两次密码不一致');
            }

            $model = new User();
            $info=$model->getOne('*',['id'=>$uid]);
            if (createPwd($opwd)!==$info['pwd']){
                show(false,'旧密码不正确，请重新输入');
            }
            $data=[
                'pwd'=>createPwd($pwd)
            ];
            if ($model->updateFieldsById($info['id'],$data)){
                show(true,'更改成功');
            }
            show(true,'更改失败');
        }
        return view('',['title'=>'修改密码']);
    }

    /**
     * 消息中心
     * @return [type] [description]
     */
    public function msglist(){
        $uid = session('user.id');
        $uid = 20;

        $type  = input('?get.type') ? input('get.type') : -1; //消息类型
        $receivetime = input('?get.receivetime') ? input('get.receivetime') : -1; //消息状态
        $where = ['receiver' => $uid, 'is_delete' => '0'];
        $pageParam = [];
        if($type != -1){	
            $where['type'] = $type;
            $pageParam['query']['type']=$type;
        }

        if($receivetime != -1){
            if ($receivetime == 1) $where['receivetime'] = ['gt',0];
            if ($receivetime == 2) $where['receivetime'] = 0;
            $pageParam['query']['receivetime']=$receivetime;
        }
        $this->assign('type',$type);
        $this->assign('receivetime',$receivetime);

        $message = new Message();

        $data = $message->msgLists($where,15,$pageParam);
        foreach ($data as &$vo){
            $msgLink = $vo['link'];
            if (!empty($msgLink)){
                $link = json_decode($msgLink,true);
                if (isset($link['pc'])) $vo['link'] = $link['pc']; else $vo['link'] = '';
            }else{
                $vo['link'] = '';
            }
        }

        $num = $message->getUnreadMsgNum($uid);
        return view('',['data'=>$data,'title'=>'消息列表','num'=>$num]);
    }

    /**
     * 标记消息为已读
     * @return [type] [description]
     */
    public function mread(){
        if (request()->isAjax()){
            $id = input('post.id');
            $msgModel = new Message();
            $res = $msgModel->readMsgById($id);
            if ($res) show(1,'修改成功');
            show(0,'修改失败');
        }
    }

    /**
     * 删除消息
     * @return [type] [description]
     */
    public function delmsg(){
        if (request()->isAjax()){
            $id = input('post.id');
            $msgModel = new Message();
            $res = $msgModel->delMsgById($id);
            if ($res) show(1,'删除成功');
            show(0,'删除失败');
        }
    }




}
